package top.itmeans.yes.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.itmeans.yes.common.api.CommonResult;
import top.itmeans.yes.api.CommonPage;
import top.itmeans.yes.dto.*;
import top.itmeans.yes.entity.Order;
import top.itmeans.yes.service.OrderService;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器 OMS
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * DELETE [host:ip/serverPath]/order?ids=xxx
     */
    @ApiOperation("批量删除订单")
    @DeleteMapping
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = orderService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * PUT [host:ip/serverPath]/order/delivery
     */
    @ApiOperation("发货")
    @PutMapping(value = "/delivery")
    public CommonResult delivery(@RequestBody List<OrderDeliveryParam> deliveryParamList) {
        int count = orderService.delivery(deliveryParamList);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * PUT [host:ip/serverPath]/order/close?ids=xxx&note="xxx"
     */
    @ApiOperation("批量关闭订单")
    @PutMapping(value = "/close")
    public CommonResult close(@RequestParam("ids") List<Long> ids, @RequestParam String note) {
        boolean flag = orderService.close(ids, note);
        if (flag) {
            return CommonResult.success(true);
        }
        return CommonResult.failed();
    }

    /**
     * PUT [host:ip/serverPath]/order/receiverInfo
     * @param receiverInfoParam
     */
    @ApiOperation("修改收货人信息")
    @PutMapping(value = "/receiverInfo")
    public CommonResult updateReceiverInfo(@RequestBody ReceiverInfoParam receiverInfoParam) {
        int count = orderService.receiverInfoParam(receiverInfoParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * PUT [host:ip/serverPath]/order/moneyInfo
     * @param moneyInfoParam
     */
    @ApiOperation("修改订单费用信息")
    @PutMapping(value = "/moneyInfo")
    public CommonResult updateReceiverInfo(@RequestBody MoneyInfoParam moneyInfoParam) {
        int count = orderService.updateMoneyInfo(moneyInfoParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * PUT [host:ip/serverPath]/order/note?id=xxx&note=xxx&status=xxx
     */
    @ApiOperation("备注订单")
    @PutMapping(value = "/note")
    public CommonResult updateNote(@RequestParam("id") Long id,
                                   @RequestParam("note") String note,
                                   @RequestParam("status") Integer status) {
        int count = orderService.updateNote(id, note, status);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    /**
     * GET [host:ip/serverPath]/order/list?pageSize=xxx&pageNum=xxx&queryParam
     */
    @ApiOperation("查询订单")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<Order>> list(OrderQueryParam queryParam,
                                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<Order> page = orderService.list(queryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * GET [host:ip/serverPath]/order/{id}
     */
    @ApiOperation("获取订单详情：订单信息、商品信息、操作记录")
    @GetMapping(value = "/{id}")
    public CommonResult<OrderDetail> detail(@PathVariable Long id) {
        OrderDetail orderDetailResult = orderService.detail(id);
        return CommonResult.success(orderDetailResult);
    }
}
